package br.com.app.ligasoccer;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Toast;
import br.com.app.ligasoccer.dao.LigaDAO;
import br.com.app.ligasoccer.domain.Liga;
import br.com.app.ligasoccer.util.Constantes;

public class LigaActivity extends Activity implements OnDateSetListener {
	
	private EditText liga, jogadores, jogos;
	private Button dataInicio;
	private int dia, mes, ano;
	private Date ligaDataInicio;
	private SimpleDateFormat formatter;
	private LigaDAO ligaDAO;
	private Calendar calendar;
	private String id;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.nova_liga);
		
		this.formatter = new SimpleDateFormat("dd/MM/yyyy");
		this.ligaDAO = new LigaDAO(this);
		this.calendar = Calendar.getInstance();
		
		this.ano = this.calendar.get(Calendar.YEAR);
		this.mes = this.calendar.get(Calendar.MONTH);
		this.dia = this.calendar.get(Calendar.DAY_OF_MONTH);
		
		this.liga = (EditText) findViewById(R.id.liga);
		this.jogadores = (EditText) findViewById(R.id.jogadores);
		this.jogos = (EditText) findViewById(R.id.jogos);
		this.dataInicio = (Button) findViewById(R.id.dataInicio);
		
		this.ligaDataInicio = new Date();
		this.dataInicio.setText(this.formatter.format(this.ligaDataInicio));
		
		Intent intent = getIntent();
		if (intent.hasExtra(Constantes.EXTRA_ID_LIGA)) {
			this.id = intent.getStringExtra(Constantes.EXTRA_ID_LIGA);
			this.preparaEdicao();
		}
	}
	
	public void selecionarData(View view) {
		this.showDialog(view.getId());
	}
	
	@Override
	protected Dialog onCreateDialog(int id) {
		// TODO Auto-generated method stub
		if (id == R.id.dataInicio) {
			return new DatePickerDialog(this, this, this.ano, this.mes, this.dia);
		}
		return null;
	}

	@Override
	public void onDateSet(DatePicker view, int year, int monthOfYear,
			int dayOfMonth) {
		// TODO Auto-generated method stub
		this.ligaDataInicio = this.criaDataInicial(year, monthOfYear, dayOfMonth);
		this.dataInicio.setText(this.formatter.format(this.ligaDataInicio));
	}
	
	private Date criaDataInicial(int anoSelecionado, int mesSelecionado, int diaSelecionado) {
		calendar.set(anoSelecionado, mesSelecionado, diaSelecionado);
		return calendar.getTime();
	}
	
	public void salvarLiga(View view) {
		String nomeLiga = this.liga.getText().toString(),
				qtdJogadores = this.jogadores.getText().toString(),
				qtdJogos = this.jogos.getText().toString();
		
		if (nomeLiga.trim().equals("") || qtdJogadores.trim().equals("") || qtdJogos.trim().equals("")) {
			Toast.makeText(this, R.string.mensagem_campos_vazios, Toast.LENGTH_SHORT).show();
			return;
		} else if (this.ligaDAO.carregarPorNome(nomeLiga) != null) {
			Toast.makeText(this, R.string.mensagem_liga_existe, Toast.LENGTH_SHORT).show();
			return;
		}
		Liga liga = new Liga(nomeLiga, Integer.valueOf(qtdJogadores), Integer.valueOf(qtdJogos), this.ligaDataInicio);
		if (this.id != null) {
			liga.setId(Integer.valueOf(this.id));
		}
		if (this.ligaDAO.salvarLiga(liga)) {
			Toast.makeText(this, R.string.registro_salvo, Toast.LENGTH_SHORT).show();
			finish();
		} else {
			Toast.makeText(this, R.string.erro_salvar, Toast.LENGTH_SHORT).show();
		}
	}
	
	private void preparaEdicao() {
		Liga liga = this.ligaDAO.carregarLiga(this.id);
		if (liga != null) {
			this.liga.setText(liga.getNome());
			this.jogadores.setText(liga.getQuantidadeDeJogadores().toString());
			this.jogos.setText(liga.getQuantidadeDeJogos().toString());
			
			this.ligaDataInicio = liga.getDataDeInicio();
			this.dataInicio.setText(this.formatter.format(this.ligaDataInicio));
		}
	}
}
